
#include <iostream>
#include "f_extern.h"

using namespace std;


int make_file(string host_file,int head_block)
{

	char name[10];
	int num_next = 0;
	int i,j,size;
	int next_block;
	header head;
	header new_file;

	string path;
	string chld_path;
	FILE *ptr;
	FILE *fd;
	char *blob = new char[block_size - sizeof(int)];

	sprintf(name,"./.%d",head_block);
	path = root_dir; path.append(name);
	fd = fopen(path.data(),"rb");
	fread(&head,sizeof(header),1,fd);
	fclose(fd);
	size = head.size;
	next_block = head.data_block;

	ptr = fopen(host_file.data(),"wb");
	if(ptr ==NULL )
	{
		fclose(fd);
		return -1;
	}
	while(size>0)
	{
		sprintf(name,"./.%d",next_block);
		path = root_dir; path.append(name);
		fd = fopen(path.data(),"rb");

		if(size>=(block_size-sizeof(int)))
		{
			
			fread(blob,(block_size-sizeof(int)),1,fd);
			fwrite(blob,(block_size-sizeof(int)),1,ptr);
		}
		else
		{
			
			fread(blob,size,1,fd);
			fwrite(blob,size,1,ptr);
		}
		
		fseek(fd,-sizeof(int),SEEK_END);
		fread(&next_block,sizeof(int),1,fd);
		size -= (block_size-sizeof(int));
		fclose(fd);
	}
	fclose(ptr);
	return 0;
}
int find_dir(string dir_name, int base_block,int type )
{
	char name[10];
	int num_next = 0;
	int i,j;
	int num_chld;
	int cur_block_size,last_block_size;
	int tmp_size;
	header head;
	header new_dir;
	sprintf(name,"./.%d",base_block);
	string path = root_dir; path.append(name);
	string chld_path;
	FILE *ptr;
	FILE *fd = fopen(path.data(),"rb");
	fread(&head,sizeof(header),1,fd);
	fclose(fd);
	num_next = head.data_block;	
	tmp_size = head.size;
	do
	{
		sprintf(name,"./.%d",num_next);
		path = root_dir; path.append(name);
		fd = fopen(path.data(),"rb");
		for(j=0;j<head.size && j<block_size-(sizeof(int)); j+=sizeof(int))
		{	
			fread(&num_chld,sizeof(int),1,fd);
			sprintf(name,"./.%d",num_chld);
			chld_path = root_dir; chld_path.append(name);
			
			ptr = fopen(chld_path.data(),"rb");
			fread(&new_dir,sizeof(header),1,ptr);
			
			fclose(ptr);
			
			if(strcmp(new_dir.name,dir_name.data())==0 && new_dir.type ==type)
			{
				fclose(fd);
				return num_chld;
			}
		}
		head.size -= (block_size-sizeof(int));
		fseek(fd,-sizeof(int),SEEK_END);
		fread(&num_next,1,sizeof(int),fd);
		fclose(fd);
	}while(num_next!=0);
	

	return -1;
}

int main(int argc, char* argv[])
{	
	char* _f_ = "[export]";
	int next_dir_num = 0;
	int i;
	FILE* config = NULL; 
	FILE* block = NULL;
	string file_name;
	string host_file;
	header dir;
	string dir_path;
	if (argc != 4){
#ifdef PRINT_MSG 
		printf("%s: Use export root fs_file host_file\n",_f_); 
#endif
	return 2;}

		root_dir = argv[1];
		make_build_map(1);

		host_file = argv[3];
		dir_path = argv[2];
		parse_path(dir_path);
	
		next_dir_num = 1;
		for( i = 0;i<dir_set.size()-1;i++)
		{
			next_dir_num = find_dir(dir_set[i],next_dir_num,0);
			if(next_dir_num<0)
			{
#ifdef PRINT_MSG
	printf("%s: Directory/file %s is not exist.\n",_f_,dir_set[i].data());
#endif	
			return 3;
			}
		}
		next_dir_num = find_dir(dir_set[dir_set.size()-1],next_dir_num,1);
		if(next_dir_num<0)	
		{
#ifdef PRINT_MSG
	printf("%s: Directory/file %s is not exist.\n",_f_,dir_set[dir_set.size()-1].data());
#endif
			return 4;
		}
		if(make_file(host_file, next_dir_num) <0) 
		{
#ifdef PRINT_MSG
	printf("%s: File write error.\n",_f_);
#endif	
			return 5;
		}
		else return 0;
}

